﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using DataModel;

namespace MetaDesigner
{
    /// <summary>
    /// Interaction logic for TermPicker.xaml
    /// </summary>
    public partial class TermPicker : Window
    {
        TextBox tb;
        TermSetList termSets = SaveFile.termSets;


        /// <summary>
        /// Initializes a new instance of the <see cref="TermPicker"/> class.
        /// </summary>
        /// <param name="tb">The tb.</param>
        public TermPicker(TextBox tb)
        {
            this.tb = tb;
            InitializeComponent();
            PopulateTree();
            PopulateSelectedBox();

            if (TermTree.Items.Count == 0)
            {
                Label l = new Label();
                l.Content = "It appears you haven't defined any Terms yet.\nGo to the Managed Metadata tab to create some!";
                ((Grid)TermTree.Parent).Children.Add(l);
                Grid.SetColumn(l, 0);
                Grid.SetRow(l, 1);
                l.Margin = new Thickness(10);
            }
        }

        /// <summary>
        /// Populates the tree.
        /// </summary>
        private void PopulateTree()
        {            
            foreach (TermSet ts in termSets)
            {
                TreeViewItem tstvi = new TreeViewItem();
                tstvi.Header = ts.Name;
                tstvi.Tag = ts;
                tstvi.FontSize = 20;
                tstvi.FontWeight = FontWeights.Bold;
                foreach (Term t in ts)
                {
                    TreeViewItem tvi = new TreeViewItem();
                    FillTVI(tvi, t);
                    tstvi.Items.Add(tvi);
                }
                TermTree.Items.Add(tstvi);
            }
        }

        /// <summary>
        /// Fills the tree view item with the term's info.
        /// </summary>
        /// <param name="tvi">The tvi.</param>
        /// <param name="t">The t.</param>
        private void FillTVI(TreeViewItem tvi, Term t)
        {
            tvi.Header = t.Name;
            tvi.Tag = t;
            tvi.IsExpanded = true;
            tvi.FontStyle = (t.IsTaggable) ? FontStyles.Normal : FontStyles.Oblique;
            tvi.FontWeight = (t.IsTaggable) ? FontWeights.Bold : FontWeights.Normal;
            tvi.FontSize = 14;
            foreach (Term t2 in t)
            {
                TreeViewItem tvi2 = new TreeViewItem();
                FillTVI(tvi2, t2);
                tvi.Items.Add(tvi2);
            }
        }

        /// <summary>
        /// Populates the selected item box.
        /// </summary>
        private void PopulateSelectedBox()
        {
            try
            {
                SelectedTerm.Text = ((TermSet)tb.Tag).Name;
            }
            catch (Exception ex)
            {
                SelectedTerm.Text = "";
                Log.Append("PopulateSelectedBox Exception:").Append(ex.Message).AppendLine();
            }
        }

        /// <summary>
        /// Handles the SelectedItemChanged event of the TermTree control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RoutedPropertyChangedEventArgs{System.Object}"/> instance containing the event data.</param>
        private void TermTree_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            TreeViewItem tvi = (TreeViewItem)TermTree.SelectedItem;
            StringBuilder msg = new StringBuilder("");
            try
            {
                if (tvi != null && tb.Tag != tvi.Tag)
                {
                    Select.IsEnabled = true;
                }
                else
                {
                    Select.IsEnabled = false;
                }
            }
            catch (InvalidCastException ivc)
            {
                Log.Append("TermTree_SelectedItemChanged Exception: ").Append(ivc.Message).AppendLine();
                Select.IsEnabled = false;
                //oops, well that's what happens when you select a root node (Tagges with a TermSet...)
            }
        }

        /// <summary>
        /// Handles the Click event of the Select control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
        private void Select_Click(object sender, RoutedEventArgs e)
        {
            TreeViewItem tvi = (TreeViewItem)TermTree.SelectedItem;
            if (tvi != null)
            {
                tb.Tag = (TermSet)tvi.Tag;
            }
            PopulateSelectedBox();
        }

        /// <summary>
        /// Handles the Click event of the submitButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
        private void submitButton_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// Handles the Closed event of the Window control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void Window_Closed(object sender, EventArgs e)
        {
            ((MainWindow)(this.Owner)).UpdateManagedMetaDataTab(); 
            ((MainWindow)this.Owner).UpdateSiteColumnAdditionalInfoMetadataTextBox();
        }
    }
}
